package cn.btks.resultintegration.rule.service.impl;

import cn.btks.commonserver.pojo.PageList;
import cn.btks.commonserver.result.ResultState;
import cn.btks.commonserver.result.StateInfo;
import cn.btks.resultintegration.rule.pojo.EnvironmentResourceRule;
import cn.btks.resultintegration.rule.repository.EnvironmentResourceRuleRepository;
import cn.btks.resultintegration.rule.service.EnvironmentResourceRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author lijun
 * @Date 2021/8/1 15:12
 */
@Service
public class EnvironmentResourceRuleServiceImpl implements EnvironmentResourceRuleService {

    @Autowired
    EnvironmentResourceRuleRepository environmentResourceRuleRepository;

    @Override
    public EnvironmentResourceRule findByCode(String code) {
        return environmentResourceRuleRepository.findByCode(code);
    }

    @Override
    public ResultState findPages(Integer pageNumber, Integer pageSize, String division, String code) {
        PageRequest pageRequest = PageRequest.of(pageNumber - 1, pageSize);
        Specification<EnvironmentResourceRule> specification = new Specification<EnvironmentResourceRule>() {
            @Override
            public Predicate toPredicate(Root<EnvironmentResourceRule> root,
                                         CriteriaQuery<?> criteriaQuery,
                                         CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if(!StringUtils.isEmpty(division)){
                    predicates.add(criteriaBuilder.equal(root.get("division"),division));
                }
                if(!StringUtils.isEmpty(code)){
                    predicates.add(criteriaBuilder.like(root.get("code"),"%" + code + "%"));
                }
                Predicate[] predicates1 = new Predicate[predicates.size()];
                return criteriaBuilder.and(predicates.toArray(predicates1));
            }
        };
        Page< EnvironmentResourceRule> configPage = environmentResourceRuleRepository.findAll(specification, pageRequest);
        if (configPage.getTotalElements() > 0) {
            PageList pageList = new PageList();
            pageList.setResult(configPage.getContent());
            pageList.setPageNum(configPage.getTotalPages());
            pageList.setTotalCount(configPage.getTotalElements());
            return new ResultState(StateInfo.FIND_OK.getCode(), StateInfo.FIND_OK.getMsg(), pageList);
        }
        return new ResultState(StateInfo.FIND_NULL.getCode(), StateInfo.FIND_NULL.getMsg());
    }
}
